/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package questao14;

import com.towel.math.Expression;


/**
 *
 * @author SDKW
 */
public class ItemCalculadora {

    protected String operacao;
    protected ItemCalculadoraCaretaker caretaker;

    public ItemCalculadora() {
        this.caretaker = new ItemCalculadoraCaretaker();
    }
    public ItemCalculadora(String operacao) {
        this.caretaker = new ItemCalculadoraCaretaker();
        this.operacao = operacao;
    }

    public void adicionarOperacao(ItemCalculadora operacao) {
        if (operacao.getOperacao() != null) {
            caretaker.adicionarMemento(new ItemCalculadoraMemento(operacao));
        }
    }

    protected String getOperacao() {
        return operacao;
    }

    public void desfazerUltimaOperacao() {
        caretaker.getUltimoEstadoSalvo();
    }

    public void mostrarExpressao() {
        System.out.println(caretaker.mostraExpressao());
    }
    
    public void validaExpressao(){
        try {
            caretaker.validaExpressao();
            System.out.println("Expressão validada!");
        } catch (Exception ex) {
            System.out.println(ex.getMessage());         
        }
    }
    
    public void calculaExpressao(){
        Expression exp = new Expression(caretaker.mostraExpressao());  
        double result = exp.resolve();  
        System.out.println("Resultado : " + result); 
    }
}
